<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-input
          v-model="queryParams.role"
          placeholder="请输入角色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录名" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加密盐" prop="salt">
        <el-input
          v-model="queryParams.salt"
          placeholder="请输入加密盐"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="identityCardNum">
        <el-input
          v-model="queryParams.identityCardNum"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登陆成功获得token" prop="token">
        <el-input
          v-model="queryParams.token"
          placeholder="请输入登陆成功获得token"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker clearable
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录IP" prop="loginIp">
        <el-input
          v-model="queryParams.loginIp"
          placeholder="请输入最后登录IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLoginTime">
        <el-date-picker clearable
          v-model="queryParams.lastLoginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="登录次数" prop="loginCount">
        <el-input
          v-model="queryParams.loginCount"
          placeholder="请输入登录次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开放平台unionId" prop="unionId">
        <el-input
          v-model="queryParams.unionId"
          placeholder="请输入开放平台unionId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公众号openId" prop="mpOpenId">
        <el-input
          v-model="queryParams.mpOpenId"
          placeholder="请输入公众号openId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小程序openId" prop="appletOpenId">
        <el-input
          v-model="queryParams.appletOpenId"
          placeholder="请输入小程序openId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="APPopenId" prop="appOpenId">
        <el-input
          v-model="queryParams.appOpenId"
          placeholder="请输入APPopenId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="wechatNo">
        <el-input
          v-model="queryParams.wechatNo"
          placeholder="请输入微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="QQ号码" prop="qqNo">
        <el-input
          v-model="queryParams.qqNo"
          placeholder="请输入QQ号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总积分" prop="totalIntegral">
        <el-input
          v-model="queryParams.totalIntegral"
          placeholder="请输入总积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余积分" prop="remainIntegral">
        <el-input
          v-model="queryParams.remainIntegral"
          placeholder="请输入剩余积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级用户ID" prop="parentMemberId">
        <el-input
          v-model="queryParams.parentMemberId"
          placeholder="请输入上级用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级用户名称" prop="parentMemberName">
        <el-input
          v-model="queryParams.parentMemberName"
          placeholder="请输入上级用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请码" prop="invitationCode">
        <el-input
          v-model="queryParams.invitationCode"
          placeholder="请输入邀请码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="余额" prop="balance">
        <el-input
          v-model="queryParams.balance"
          placeholder="请输入余额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后操作时间" prop="lastOperateTime">
        <el-date-picker clearable
          v-model="queryParams.lastOperateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:base:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:base:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:base:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:base:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="memberId" />
      <el-table-column label="ID" align="center" prop="userId" />
      <el-table-column label="角色" align="center" prop="role" />
      <el-table-column label="用户名" align="center" prop="memberName" />
      <el-table-column label="用户头像" align="center" prop="headPortrait" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.headPortrait" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="登录名" align="center" prop="loginName" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="加密盐" align="center" prop="salt" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center" prop="identityCardNum" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="登陆成功获得token" align="center" prop="token" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="最后登录IP" align="center" prop="loginIp" />
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录次数" align="center" prop="loginCount" />
      <el-table-column label="开放平台unionId" align="center" prop="unionId" />
      <el-table-column label="公众号openId" align="center" prop="mpOpenId" />
      <el-table-column label="小程序openId" align="center" prop="appletOpenId" />
      <el-table-column label="APPopenId" align="center" prop="appOpenId" />
      <el-table-column label="用户状态(N=正常,D=删除)" align="center" prop="memberStatus" />
      <el-table-column label="微信号" align="center" prop="wechatNo" />
      <el-table-column label="QQ号码" align="center" prop="qqNo" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="总积分" align="center" prop="totalIntegral" />
      <el-table-column label="剩余积分" align="center" prop="remainIntegral" />
      <el-table-column label="上级用户ID" align="center" prop="parentMemberId" />
      <el-table-column label="上级用户名称" align="center" prop="parentMemberName" />
      <el-table-column label="邀请码" align="center" prop="invitationCode" />
      <el-table-column label="邀请码二维码" align="center" prop="invitationQrCode" />
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="最后操作时间" align="center" prop="lastOperateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastOperateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:base:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:base:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入ID" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="用户名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户头像" prop="headPortrait">
          <image-upload v-model="form.headPortrait"/>
        </el-form-item>
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="加密盐" prop="salt">
          <el-input v-model="form.salt" placeholder="请输入加密盐" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="identityCardNum">
          <el-input v-model="form.identityCardNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="登陆成功获得token" prop="token">
          <el-input v-model="form.token" placeholder="请输入登陆成功获得token" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="最后登录IP" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入最后登录IP" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="lastLoginTime">
          <el-date-picker clearable
            v-model="form.lastLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="登录次数" prop="loginCount">
          <el-input v-model="form.loginCount" placeholder="请输入登录次数" />
        </el-form-item>
        <el-form-item label="开放平台unionId" prop="unionId">
          <el-input v-model="form.unionId" placeholder="请输入开放平台unionId" />
        </el-form-item>
        <el-form-item label="公众号openId" prop="mpOpenId">
          <el-input v-model="form.mpOpenId" placeholder="请输入公众号openId" />
        </el-form-item>
        <el-form-item label="小程序openId" prop="appletOpenId">
          <el-input v-model="form.appletOpenId" placeholder="请输入小程序openId" />
        </el-form-item>
        <el-form-item label="APPopenId" prop="appOpenId">
          <el-input v-model="form.appOpenId" placeholder="请输入APPopenId" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechatNo">
          <el-input v-model="form.wechatNo" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="QQ号码" prop="qqNo">
          <el-input v-model="form.qqNo" placeholder="请输入QQ号码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总积分" prop="totalIntegral">
          <el-input v-model="form.totalIntegral" placeholder="请输入总积分" />
        </el-form-item>
        <el-form-item label="剩余积分" prop="remainIntegral">
          <el-input v-model="form.remainIntegral" placeholder="请输入剩余积分" />
        </el-form-item>
        <el-form-item label="上级用户ID" prop="parentMemberId">
          <el-input v-model="form.parentMemberId" placeholder="请输入上级用户ID" />
        </el-form-item>
        <el-form-item label="上级用户名称" prop="parentMemberName">
          <el-input v-model="form.parentMemberName" placeholder="请输入上级用户名称" />
        </el-form-item>
        <el-form-item label="邀请码" prop="invitationCode">
          <el-input v-model="form.invitationCode" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item label="邀请码二维码" prop="invitationQrCode">
          <el-input v-model="form.invitationQrCode" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入余额" />
        </el-form-item>
        <el-form-item label="最后操作时间" prop="lastOperateTime">
          <el-date-picker clearable
            v-model="form.lastOperateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后操作时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBase, getBase, delBase, addBase, updateBase } from "@/api/member/base"

export default {
  name: "Base",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员列表表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        role: null,
        memberName: null,
        headPortrait: null,
        loginName: null,
        password: null,
        salt: null,
        realName: null,
        identityCardNum: null,
        phone: null,
        token: null,
        gender: null,
        birthday: null,
        province: null,
        address: null,
        loginIp: null,
        lastLoginTime: null,
        loginCount: null,
        unionId: null,
        mpOpenId: null,
        appletOpenId: null,
        appOpenId: null,
        memberStatus: null,
        wechatNo: null,
        qqNo: null,
        totalIntegral: null,
        remainIntegral: null,
        parentMemberId: null,
        parentMemberName: null,
        invitationCode: null,
        invitationQrCode: null,
        balance: null,
        sex: null,
        lastOperateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询会员列表列表 */
    getList() {
      this.loading = true
      listBase(this.queryParams).then(response => {
        this.baseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        userId: null,
        role: null,
        memberName: null,
        headPortrait: null,
        loginName: null,
        password: null,
        salt: null,
        realName: null,
        identityCardNum: null,
        phone: null,
        token: null,
        gender: null,
        birthday: null,
        province: null,
        address: null,
        loginIp: null,
        lastLoginTime: null,
        loginCount: null,
        unionId: null,
        mpOpenId: null,
        appletOpenId: null,
        appOpenId: null,
        memberStatus: null,
        wechatNo: null,
        qqNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        totalIntegral: null,
        remainIntegral: null,
        parentMemberId: null,
        parentMemberName: null,
        invitationCode: null,
        invitationQrCode: null,
        balance: null,
        sex: null,
        lastOperateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加会员列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const memberId = row.memberId || this.ids
      getBase(memberId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改会员列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            updateBase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addBase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids
      this.$modal.confirm('是否确认删除会员列表编号为"' + memberIds + '"的数据项？').then(function() {
        return delBase(memberIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/base/export', {
        ...this.queryParams
      }, `base_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
